package br.edu.fatec.ratoninja;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

	private static final String TAG = "RatoNinja::GameView";
	private final Bitmap blackTile = BitmapFactory.decodeResource(getResources(), R.drawable.black);
	private final Bitmap whiteTile = BitmapFactory.decodeResource(getResources(), R.drawable.white);
	private final Bitmap orangeTile = BitmapFactory.decodeResource(getResources(), R.drawable.orange);
	private final Bitmap greenTile = BitmapFactory.decodeResource(getResources(), R.drawable.green);

	private GameLevel gameLevel;
	private GameLoop gameLoop;
	private Player player;

	public GameView(Context context) {
		super(context);
		getHolder().addCallback(this);

		setBackgroundColor(Color.WHITE);
		setFocusable(true);

		init();

		gameLoop = new GameLoop(this, getHolder());
	}

	@Override
	protected void onDraw(Canvas canvas) {
		//Log.i(TAG, "onDraw Canvas");
		//canvas.save();
		for (Tile tile : gameLevel.getMap()) {
			if (tile.getType() != '3')
			tile.draw(canvas);
			//canvas.drawBitmap(tile.getBitmap(), tile.getPosition().getX(), tile.getPosition().getY(), null);
		}
		
		for (Tile tile : gameLevel.getMap()) {
			if (tile.getType() == '3')
				tile.draw(canvas);
			//canvas.drawBitmap(tile.getBitmap(), tile.getPosition().getX(), tile.getPosition().getY(), null);
		}
		Log.i(TAG, "posix " + player.getPosition().getX());
		//canvas.drawBitmap(player.getBitmap(), player.getPosition().getX(), player.getPosition().getY(), null);
		//canvas.restore();
	}

	public void updatePhysics() {
		// TODO: fazer depois
	}

	@SuppressWarnings("unused")
	public void init() {
		try {
			InputStreamReader input = new InputStreamReader(GameView.class.getResourceAsStream("map.txt"));
			if (input == null) {
				System.out.println("Maps null!!");
				System.exit(0);
			}
			BufferedReader reader = new BufferedReader(input);

			String line = reader.readLine();
			StringTokenizer tokenizer = new StringTokenizer(line, " ");

			int width = Integer.parseInt(tokenizer.nextToken());
			int height = Integer.parseInt(tokenizer.nextToken());

			reader.mark(width + 1);

			// inicializar o GameLevel
			gameLevel = new GameLevel(width, height);
			char tile;
			for (int i = 0; i < height; i++) {
				line = reader.readLine();
				for (int j = 0; j < width; j++) {
					tile = line.charAt(j);
					if (tile == '0') {
						gameLevel.getMap().add(new Tile(whiteTile, new Position(j * 16, i * 16), true));
					} else if (tile == '1') {
						gameLevel.getMap().add(new Tile(blackTile, new Position(j * 16, i * 16), true));
					} else if (tile == '2') {
						gameLevel.getMap().add(new Tile(orangeTile, new Position(j * 16, i * 16), true));
					}else if (tile == '3') {
						player = new Player(greenTile,new Position(j * 16, i * 16), false);
						gameLevel.getMap().add(player);
					}
				}
			}

			//player = new Player(greenTile, new Position(4, 13 * 16), false);
			//gameLevel.getMap().add(player);

			reader.close();
			input.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		gameLoop.setRunning(true);
		gameLoop.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		gameLoop.setRunning(false);

		while (retry) {
			try {
				gameLoop.join();
				retry = false;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		player.getPosition().setX(player.getPosition().getX() + player.getSpeed().getX());
		Log.i(TAG, "onTouch: posix = " + player.getPosition().getX());
		return super.onTouchEvent(event);
	}
}
